package cn.ffcs.bpmpvc.utils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.echoice.util.DateUtil;

import cn.ffcs.bpm.model.ProcTranRecord;
import cn.ffcs.bpmpvc.graph.Node;
import cn.ffcs.bpmpvc.svc.TaskAssignBean;
import cn.ffcs.bpmpvc.svc.TaskService;
import cn.ffcs.bpmpvc.web.views.ProcessInstView;
import cn.ffcs.bpmpvc.web.views.TaskView;
import cn.ffcs.businessrequest.model.ReqDocu;
import cn.ffcs.organ.model.Person;
import cn.ffcs.system.dict.model.Dict;
import cn.ffcs.util.context.ApplicationContextUtil;

public class FlowContextUtil {
	public static final String JSP_SUCCESS="/bpm/pvc/jumpMsg.jsp";
	public static void judgeDocuIsUpload(TaskView taskView, List<ReqDocu> reqDocuList) {
		List<Dict> dictList = taskView.getDocumentTypes();
		int taskId = taskView.getProcTranRecord().getId();
		if (dictList != null && dictList.size() > 0) {
			for (Dict dict : dictList) {
				int typeId = dict.getId();
				for (ReqDocu reqDocu : reqDocuList) {
					if (reqDocu.getRecordId() != null && reqDocu.getRecordId() == taskId) {
						if (typeId == reqDocu.getDocType().getId()) {
							dict.setRequire(false);
							break;
						}
					}
				}
			}
		}
	}

	public static void setRepDocTableStyleList(List<ReqDocu> list) {
		if (list != null && list.size() > 0) {
			ReqDocu tmp = list.get(0);
			int count = 0;
			for (ReqDocu reqDocu : list) {
				// reqDocu.setLength("0");
				// 判断是否同样的步骤
				if (tmp.getProcessDetail() != null && reqDocu.getProcessDetail() != null && StringUtils.isNotBlank(tmp.getProcessDetail().getName())
						&& StringUtils.isNotBlank(reqDocu.getProcessDetail().getName())) {
					if (tmp.getProcessDetail().getName().equals(reqDocu.getProcessDetail().getName())) {
						reqDocu.setVersion("1");
						count++;
					} else {
						tmp.setLength(String.valueOf(count));
						count = 1;
						tmp = reqDocu;
					}
				} else {
					tmp.setLength(String.valueOf(count));
					count = 1;
					tmp = reqDocu;
				}
			}
			tmp.setLength(String.valueOf(count));
		}
	}
	/**
	 * 取得下一步
	 * @param req
	 * @return
	 */
	public static List<TaskAssignBean> getNextTaskUserList(HttpServletRequest request) {
		List<TaskAssignBean> nextTasksList = new ArrayList<TaskAssignBean>();
		String taskNodeId[] = request.getParameterValues("taskNodeId");
		String userId[] = request.getParameterValues("userId");
		if (taskNodeId != null && taskNodeId.length > 0) {
			for (int i = 0; i < taskNodeId.length; i++) {
				TaskAssignBean assignBean = new TaskAssignBean();
				assignBean.setTaskNodeId(taskNodeId[i]);
				assignBean.setUserId(userId[i]);
				nextTasksList.add(assignBean);
			}
		}
		return nextTasksList;
	}
	
	
	public static boolean isCommit(HttpServletRequest request) {
		String isCommitS=request.getParameter("isCommit");
	    boolean isCommit=Boolean.valueOf(isCommitS);
	    return isCommit;
	}
	
    
	public static String setStartFlowMsg(boolean isCommit) {
		String returnMsg=null;
        if(isCommit){
            returnMsg="业务需求单提交成功";
        }else{
            returnMsg="业务需求单记录保存成功";
        }
        return returnMsg;
	}
	
	
	public static String setCommitTaskMsg(boolean isCommit) {
		String returnMsg=null;
        if(isCommit){
            returnMsg="待办单提交成功";
        }else{           
            returnMsg="待办单记录保存成功";            
        }
        return returnMsg;
	}
	/**
	 * 设置返回消息到request中
	 * @param request
	 * @param returnMsg
	 */
	public static void setReturnMsgAttr(HttpServletRequest request,String returnMsg) {
		request.setAttribute("returnMsg", returnMsg);
	}
	
	/**
	 * 设置转向URL到request中
	 * @param request
	 * @param redirectUrl
	 */
	public static void setRedirectUrlAttr(HttpServletRequest request,String redirectUrl) {
		request.setAttribute("redirectUrl", redirectUrl);
	}
	/**
	 * 取记录结束时间
	 * @return
	 */
	public static Date getEndDate(){
		Date end=null;
		try {
			end = DateUtil.parse("9999-12-31", "yyyy-MM-dd");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return end;
	}
	
	
	public static void setProcessInstTaskInfo(TaskService taskService,ProcessInstView processInstView,String procinstId,HttpServletRequest request){
		List<Node> nodeList = taskService.getFlowPvcDao().findRunNodeList(procinstId);
		if (nodeList.size() == 0) {
			Node node = new Node();
			node.setName("结束");
			nodeList.add(node);
		}
		String nowMan="";
		request.setAttribute("runNodeList", nodeList);
		List<ProcTranRecord> list = processInstView.getFlowTaskList();
		for (int i = 0; i < list.size(); i++) {
			ProcTranRecord procTranRecord = list.get(i);
			if (procTranRecord.getTransactTime() == null&&!("03".equals(procTranRecord.getTaskType()))) {
				Person person = procTranRecord.getTransactor();
				nowMan+=person.getName()+",";
				//ApplicationContextUtil.getHttpServletRequest().setAttribute("nowMan", person.getName());
				//break;
			}
		}
		if(StringUtils.isNotBlank(nowMan)){
			nowMan=nowMan.substring(0,nowMan.length()-1);
		}
		request.setAttribute("nowMan", nowMan);
	}
}
